﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace childcare
{
    public partial class frmteacher : Form
    {
        SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        public frmteacher()
        {
            InitializeComponent();
        }

        private void lblttgiaovien_Click(object sender, EventArgs e)
        {

        }

        private void frmteacher_Load(object sender, EventArgs e)
        {
            string connect = @"Data Source=BLACK-PC\SQLEXPRESS;Initial Catalog=Childcare;Integrated Security=True";
            con = new SqlConnection(connect);
            connectdatabase.Openconnect();
            dgvgiaovien.DataSource = connectdatabase.ex("select * from teacher");
            databinding();
        }

        private void databinding()
        {
            txtmaso.DataBindings.Clear();
            txtmaso.DataBindings.Add("Text", dgvgiaovien.DataSource, "T_ID");
            txthoten.DataBindings.Clear();
            txthoten.DataBindings.Add("Text", dgvgiaovien.DataSource, "name");
            txtgioitinh.DataBindings.Clear();
            txtgioitinh.DataBindings.Add("Text", dgvgiaovien.DataSource, "sex");
            txtngaysinh.DataBindings.Clear();
            txtngaysinh.DataBindings.Add("Text", dgvgiaovien.DataSource, "date");//Convert
            txtsodt.DataBindings.Clear();
            txtsodt.DataBindings.Add("Text", dgvgiaovien.DataSource, "phone");
            txtdiachi.DataBindings.Clear();
            txtdiachi.DataBindings.Add("Text", dgvgiaovien.DataSource, "address");
        }

        // tao ham kiem tra 
        private bool test()
        {
            kt = true;
            if (txtmaso.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã số của giáo viên", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
                txtmaso.Focus();
            }
            if (txthoten.Text == "")
            {
                MessageBox.Show("Bạn phải nhập họ tên của giáo viên", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
                txthoten.Focus();
            }
            return kt;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            try
            {
                test();
                if (kt == true)
                {
                    SqlCommand commandinsert = new SqlCommand();
                    commandinsert.Connection = con;
                    commandinsert.CommandType = CommandType.Text;
                    commandinsert.CommandText = "insert teacher (T_ID,name,sex,date,phone,address) values(@T_ID,@name, @sex, @date, @phone, @address)";
                    SqlParameter pram = commandinsert.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
                    pram.Value = txtmaso.Text;
                    pram = commandinsert.Parameters.Add("@name", SqlDbType.NVarChar, 25);
                    pram.Value = txthoten.Text;
                    pram = commandinsert.Parameters.Add("@sex", SqlDbType.NVarChar, 5);
                    pram.Value = txtgioitinh.Text;
                    pram = commandinsert.Parameters.Add("@date", SqlDbType.SmallDateTime);//
                    pram.Value = txtngaysinh.Text;
                    pram = commandinsert.Parameters.Add("@phone", SqlDbType.Int);
                    pram.Value = txtsodt.Text;
                    pram = commandinsert.Parameters.Add("@address", SqlDbType.NVarChar, 100);
                    pram.Value = txtdiachi.Text;
                    con.Open();
                    commandinsert.ExecuteNonQuery();
                  //  con.Close();
                    dgvgiaovien.DataSource = connectdatabase.ex("select * from teacher");
                    MessageBox.Show("Bạn đã thêm thành công!", "Thông Báo", MessageBoxButtons.OK);
                    //databinding();
                }
            }
            catch
            {
                MessageBox.Show("Mã giáo viên này đã tồn tại! mời thoát ra để nhập lại mã giáo viên khác", "Thông Báo", MessageBoxButtons.OK);
               
            }
            finally
            {
                con.Close();
                databinding();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa không ?", "XÓA", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                SqlCommand commandelete = new SqlCommand();
                commandelete.Connection = con;
                commandelete.CommandType = CommandType.Text;
                commandelete.CommandText = @"delete from xeplop where T_ID = @T_ID";
                SqlParameter pram = commandelete.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
                pram.Value = txtmaso.Text;
                con.Open();
                commandelete.ExecuteNonQuery();
                commandelete.CommandText = @"delete from teacher where T_ID = @T_ID";
                commandelete.ExecuteNonQuery();
                con.Close();
                dgvgiaovien.DataSource = connectdatabase.ex("select * from teacher");
                MessageBox.Show("Bạn đã Xóa thành công!", "Thông Báo", MessageBoxButtons.OK);
                databinding();
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            test();
            if (kt == true)
            {
                SqlCommand commandupdate = new SqlCommand();
                commandupdate.Connection = con;
                commandupdate.CommandType = CommandType.Text;
                commandupdate.CommandText = @"update teacher set name = @name,sex = @sex,date = @date,phone = @phone,address =@address  where T_ID = @T_ID";
                SqlParameter pram = commandupdate.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
                pram.Value = txtmaso.Text;
                pram = commandupdate.Parameters.Add("@name", SqlDbType.NVarChar, 25);
                pram.Value = txthoten.Text;
                pram = commandupdate.Parameters.Add("@sex", SqlDbType.NVarChar, 5);
                pram.Value = txtgioitinh.Text;
                pram = commandupdate.Parameters.Add("@date", SqlDbType.SmallDateTime);
                pram.Value = txtngaysinh.Text;
                pram = commandupdate.Parameters.Add("@phone", SqlDbType.NVarChar, 300);
                pram.Value = txtsodt.Text;
                pram = commandupdate.Parameters.Add("@address", SqlDbType.NVarChar, 100);
                pram.Value = txtdiachi.Text;
                con.Open();
                commandupdate.ExecuteNonQuery();
                commandupdate.CommandText = @"update xeplop set name = @name where T_ID = @T_ID";
                commandupdate.ExecuteNonQuery();
                con.Close();
                dgvgiaovien.DataSource = connectdatabase.ex("select * from teacher");
                MessageBox.Show("Bạn đã Sửa thành công!", "Thông Báo", MessageBoxButtons.OK);
                databinding();
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txtmaso.Clear();
            txthoten.Clear();
            txtgioitinh.Clear();
            txtngaysinh.Clear();
            txtsodt.Clear();
            txtdiachi.Clear();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
            frmadmin _frmtrangchinh = new frmadmin();
            _frmtrangchinh.Show();
            Hide();
        }

        private void btntkhoten_Click(object sender, EventArgs e)
        {
            dt.Clear();
            SqlCommand commandsearch = new SqlCommand();
            commandsearch.Connection = con;
            commandsearch.CommandType = CommandType.Text;
            commandsearch.CommandText = "select * from teacher where name=@name or T_ID = @T_ID";
            SqlParameter pram = commandsearch.Parameters.Add("@name", SqlDbType.NVarChar, 25);
            pram.Value = txttkhoten.Text;
            pram = commandsearch.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
            pram.Value = txttkhoten.Text;
            da.SelectCommand = commandsearch;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                dgvgiaovien.DataSource = dt;
            }
            else
            {
                dgvgiaovien.DataSource = dt;
                MessageBox.Show("Không tìm thấy giáo viên này!", "THÔNG BÁO", MessageBoxButtons.OK);
            }
            databinding();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}
